package com.cxs.shopstore.controllers;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxs.shopstore.handler.ImgHandler;
import com.cxs.shopstore.models.Good;
import com.cxs.shopstore.service.GoodService;
import com.cxs.shopstore.utils.CollectionUtils;
import com.cxs.shopstore.utils.GoodUtils;
import com.cxs.shopstore.utils.ImgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxs
 * @since 2021-05-06
 */
@RestController
@RequestMapping("/good")
@SuppressWarnings("all")
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private ImgHandler imgHandler;
    @Autowired
    private GoodUtils goodUtils;
    @Autowired
    private ImgUtils imgUtils;
    @Autowired
    private CollectionUtils collectionUtils;

    /**
     * 处理器方法，分页查询数据
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/selectallgoods/{current}/{size}")
    public Map<String,Object> selectallgoods(
            @PathVariable("current") int current,
            @PathVariable("size") int size){
        Map<String,Object> map = new HashMap<>();
        IPage<Good> pagebean = new Page<>(current,size);
        IPage<Good> page = goodService.page(pagebean, new QueryWrapper<>());
        imgHandler.ByteToBase64(page);
        map.put("list",page);
        map.put("status",200);
        return map;
    }

    /**
     * 处理器方法，模糊搜索
     * @param keyword
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getgoodsByKeyWord/{keywords}/{current}/{size}")
    public Map<String,Object> getgoodsByKeyWord(
            @PathVariable("keywords") String keyword,
            @PathVariable("current") int current,
            @PathVariable("size") int size){
        Map<String,Object> map = new HashMap<>();
        IPage<Good> pagebean = new Page<>(current,size);
        QueryWrapper<Good> wrapper = new QueryWrapper<>();
        wrapper.like("good_name",keyword);
        goodService.page(pagebean, wrapper);
        imgHandler.ByteToBase64(pagebean);
        map.put("status",200);
        map.put("list",pagebean);
        return map;
    }

    /**
     * 商家发布商品
     * @param good
     * @return
     */
    @PostMapping("/saveGood")
    public Map<String,Object> saveGood(Good good){
        Map<String,Object> map = new HashMap<>();
        goodUtils.goodAdvice(good);
        QueryWrapper<Good> wrapper = new QueryWrapper<>();
        wrapper.eq("good_sku",good.getGoodSku());
        if(goodService.count(wrapper) == 0){
            if(goodService.save(good)){
                map.put("status",true);
                map.put("message","添加成功");
            }else
                map.put("status",false);
        }else{
            map.put("status",false);
            map.put("message","已经添加过该商品");
        }
        return map;
    }
    //京东搜索商品,关键字
    @GetMapping("/serachByKeywords/{keywords}/{pages}")
    public Map<String,Object> serachByKeywords(
            @PathVariable("keywords") String keywords,
            @PathVariable("pages") int pages){
        //关键字去空格
        keywords = keywords.replaceAll(" ","");
        Map<String,Object> map = new HashMap<>();
        List<Good> list = null;
        list = goodService.searchGood(keywords, pages);
        imgUtils.imgPathToBase64String(list);
        map.put("status",200);
        map.put("list",list);
        return map;
    }
    //搜索所有历史
    @GetMapping("/serachistory")
    public Map<String,Object> serachistory(){
        Map<String,Object> map = new HashMap<>();
        List<Good> list = goodService.selectall();
        imgUtils.imgPathToBase64String(list);
        map.put("status",200);
        map.put("list",list);
        return map;
    }
    //查询所有商品
    @GetMapping("/allgoods")
    public Map<String,Object> allgoodsbypage(@RequestParam("page") int current,@RequestParam("limit")int limit){
        Map<String,Object> map = new HashMap<>();
        IPage<Good> page = goodService.page(new Page<>(current, limit), new QueryWrapper<Good>());
        List<Good> list = page.getRecords();
        imgUtils.imgPathToBase64String(list);
        int count = (int) page.getTotal();
        map.put("code",0);
        map.put("data",list);
        map.put("count",count);
        map.put("msg","");
        return map;
    }

    //查询商品
    @GetMapping("/deleteById")
    public Map<String,Object> deleteById(@RequestParam("goodId") String goodId){
        Map<String,Object> map = new HashMap<>();
        boolean statu = goodService.removeById(goodId);
        map.put("status",statu);
        return map;
    }
    //批量删除
    @PostMapping("/deletebatchId")
    public Map<String,Object> deletebatchId(String list[]){
        Map<String,Object> map = new HashMap<>();
        boolean remove = goodService.removeByIds(collectionUtils.arrayToColl(list));
        map.put("status",remove);
        return map;
    }
    //修改商品信息
    @PostMapping("/updateById")
    public Map<String,Object> updateById(Good good){
        Map<String,Object> map = new HashMap<>();
        boolean status = goodService.updateById(good);
        if(status){
            map.put("status",true);
            map.put("message","修改成功");
        }else{
            map.put("status",false);
            map.put("message","修改失败");
        }
        return map;
    }
}

